package es.klad.tmp;

import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class GrabberShow implements Runnable {
	final int INTERVAL = 500;// /you may use interval
	IplImage image;
	CanvasFrame canvas = new CanvasFrame("Web Cam");

	public GrabberShow() {
		canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void run() {
		FrameGrabber grabber = new VideoInputFrameGrabber(0);
		int i = 0;
		try {
			grabber.start();
			IplImage img;
			while (true) {
				img = grabber.grab();
				if (img != null) {
					cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
					cvSaveImage((i++) + "-capture.jpg", img);
					// show image on window
					canvas.showImage(img);
				}
				Thread.sleep(INTERVAL);
			}
		} catch (Exception e) {
		}
	}
	
	public static void main(String[] args){
		Thread th = new Thread(new GrabberShow() );
		th.start();
	}
}